﻿Imports System
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.ComponentModel.Serialization
Imports System.Workflow.Runtime
Imports System.Xml

Namespace System.Workflow.Runtime.Hosting
    Public Class DefaultWorkflowLoaderService
        Inherits WorkflowLoaderService
        ' Methods
        Protected Friend Overrides Function CreateInstance(ByVal workflowType As Type) As Activity
            If (workflowType Is Nothing) Then
                Throw New ArgumentNullException("workflowType")
            End If
            If Not GetType(Activity).IsAssignableFrom(workflowType) Then
                Throw New ArgumentException(ExecutionStringManager.TypeMustImplementRootActivity)
            End If
            If (workflowType.GetConstructor(Type.EmptyTypes) Is Nothing) Then
                Throw New ArgumentException(ExecutionStringManager.TypeMustHavePublicDefaultConstructor)
            End If
            Return [TryCast](Activator.CreateInstance(workflowType),Activity)
        End Function

        Protected Friend Overrides Function CreateInstance(ByVal workflowDefinitionReader As XmlReader, ByVal rulesReader As XmlReader) As Activity
            If (workflowDefinitionReader Is Nothing) Then
                Throw New ArgumentNullException("workflowDefinitionReader")
            End If
            Dim activity As Activity = Nothing
            Dim errors As New ValidationErrorCollection
            Dim container As New ServiceContainer
            Dim service As ITypeProvider = MyBase.Runtime.GetService([Of] ITypeProvider)
            If (Not service Is Nothing) Then
                container.AddService(GetType(ITypeProvider), service)
            End If
            Dim manager As New DesignerSerializationManager(container)
            Try 
                [Using] manager.CreateSession
                    Dim serializationManager As New WorkflowMarkupSerializationManager(manager)
                    activity = [TryCast](New WorkflowMarkupSerializer().Deserialize(serializationManager, workflowDefinitionReader),Activity)
                    If ((Not activity Is Nothing) AndAlso (Not rulesReader Is Nothing)) Then
                        Dim obj2 As Object = New WorkflowMarkupSerializer().Deserialize(serializationManager, rulesReader)
                        activity.SetValue(ConditionTypeConverter.DeclarativeConditionDynamicProp, obj2)
                    End If
                    Dim obj3 As Object
                    For Each obj3 In manager.Errors
                        If TypeOf obj3 Is WorkflowMarkupSerializationException Then
                            errors.Add(New ValidationError(DirectCast(obj3, WorkflowMarkupSerializationException).Message, &H15B))
                        Else
                            errors.Add(New ValidationError(obj3.ToString, &H15B))
                        End If
                    Next
                End [Using]
            Catch exception As Exception
                errors.Add(New ValidationError(exception.Message, &H15B))
            End Try
            If errors.HasErrors Then
                Throw New WorkflowValidationFailedException(ExecutionStringManager.WorkflowValidationFailure, errors)
            End If
            Return activity
        End Function

    End Class
End Namespace

